package tr.edu.yildiz.dap.appeditor.dialogs.componentdialog;

import java.util.Comparator;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;

import tr.edu.yildiz.dap.appeditor.Activator;
import tr.edu.yildiz.dap.appeditor.componentproviders.basic.ComponentManager;
import tr.edu.yildiz.dap.appeditor.model.AppComponent;

public class ComponentDialog extends FilteredItemsSelectionDialog {
	
	AppComponent selectedAppComponent;
	private static final String DIALOG_SETTINGS = "org.eclipse.pde.ui.dialogs.PluginSelectionDialog"; //$NON-NLS-1$
	
	List<AppComponent> appComponentList;
	
	private class ComponentItemsFilter extends ItemsFilter {

		public boolean isConsistentItem(Object item) {
			return true;
		}

		public boolean matchItem(Object item) {
			String id = null;
			if (item instanceof AppComponent) {
				AppComponent unit = (AppComponent) item;
				id = unit.getId();
			}else 
				return false;

			return (matches(id));
		}

		protected boolean matches(String text) {
			String pattern = patternMatcher.getPattern();
			if (pattern.indexOf("*") != 0 & pattern.indexOf("?") != 0 & pattern.indexOf(".") != 0) {//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				pattern = "*" + pattern; //$NON-NLS-1$
				patternMatcher.setPattern(pattern);
			}
			boolean retValue = patternMatcher.matches(text);
			if (retValue){
				for (AppComponent unit : appComponentList) {
					if (unit.getId().contains(text))
						return true;
				}
				return false;
			}else 
				return false;
		}
	}

	private class DependencyComparator implements Comparator {

		public int compare(Object o1, Object o2) {
			if (!(o1 instanceof AppComponent) || !(o2 instanceof AppComponent))
				return 0;
			AppComponent appComp1 = (AppComponent) o1;
			AppComponent appComp2 = (AppComponent) o2;
			
			if (appComp1.getGroupId().equals(appComp2.getGroupId())){
				if (appComp1.getId().equals(appComp2.getId())){
					return appComp1.getVersion().compareTo(appComp2.getVersion());
				}else 
					return appComp1.getId().compareTo(appComp2.getId());
			}else 
				return appComp1.getGroupId().compareTo(appComp2.getGroupId());
		
		}

	}
	
	@Override
	protected void okPressed() {
		handleSelected(getSelectedItems());
		close();
	}
	
	public ComponentDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}
	

	public AppComponent getSelectedAppComponent() {
		return selectedAppComponent;
	}


	@Override
	protected Control createExtendedContentArea(Composite parent) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	protected ItemsFilter createFilter() {
		return new ComponentItemsFilter();
	}

	
	@Override
	protected void handleSelected(StructuredSelection selection) {
		selectedAppComponent = (AppComponent)selection.getFirstElement();
	}
	
	@Override
	protected void handleDoubleClick() {
		handleSelected(getSelectedItems());
		close();
	}

	@Override
	protected void fillContentProvider(AbstractContentProvider contentProvider,
			ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
			throws CoreException {
		
		appComponentList = ComponentManager.getInstance().getComponentList();
		progressMonitor.beginTask("Searching", appComponentList.size());
		for (AppComponent appComponent : appComponentList) {
			contentProvider.add(appComponent, itemsFilter);
			progressMonitor.worked(1);
		}
		progressMonitor.done();
		
	}


	@Override
	protected IDialogSettings getDialogSettings() {
		IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS);

		if (settings == null) {
			settings = Activator.getDefault().getDialogSettings().addNewSection(DIALOG_SETTINGS);
		}

		return settings;
	}


	@Override
	public String getElementName(Object item) {
		AppComponent appComponent = (AppComponent) item;
		return appComponent.getGroupId() + ":" + appComponent.getGroupId();
	}


	@Override
	protected Comparator getItemsComparator() {
		return new DependencyComparator();
	}


	@Override
	protected IStatus validateItem(Object item) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
